package com.xrkol.sharelib.service.impl;

import java.util.Date;
import java.util.List;

import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xrkol.sharelib.dao.ResponseStatusLogDao;
import com.xrkol.sharelib.entity.ClientType;
import com.xrkol.sharelib.entity.ResponseLogType;
import com.xrkol.sharelib.entity.ResponseStatusLog;
import com.xrkol.sharelib.service.ResponseStatusLogService;
import com.xrkol.sharelib.util.TimeUtil;
import com.google.common.base.Strings;
import com.weixin.sdk.ResponseStatus;

/**
 * @author dipei.ruan
 *
 */
@Service
@Transactional
public class ResoponseStatusLogServiceImpl implements ResponseStatusLogService {

	@Autowired
	private ResponseStatusLogDao responseStatusLogDao;
	private Logger logger = LoggerFactory
			.getLogger(ResoponseStatusLogServiceImpl.class);

	@Override
	public void logResponseStatus(ResponseStatus rs, ClientType clientType) {
		if (rs == null) {
			return;
		}
		ResponseStatusLog rlog = new ResponseStatusLog();
		JSONObject json = rs.getData();
		if (rs.isSucceed()) {
			if (json != null && isInvalid(json)) {
				rlog.setLogType(ResponseLogType.SUCCESS);
			} else {
				rlog.setLogType(ResponseLogType.INVALID);
			}
		} else {
			rlog.setLogType(ResponseLogType.FAIL);
			rlog.setErrorCode(rs.getErrorCode());
			rlog.setErrorMessage(rs.getErrorMessage());
		}
		logger.info(rs.toString());
		rlog.setJsonData(json != null ? json.toString() : "");
		rlog.setClientType(clientType);
		rlog.setTimestamp(TimeUtil.toTimestamp(new Date()));
		responseStatusLogDao.save(rlog);
	}

	@Override
	public ResponseStatusLog getResponseStatusLog(String id) {
		return responseStatusLogDao.findById(id);
	}

	@Override
	public List<ResponseStatusLog> getResponseStatusLogs(Date startDate,
			Date endDate, ResponseLogType logType, ClientType clientType,
			boolean timeASC, Integer page) {
		return responseStatusLogDao.getListByTimestamp(
				TimeUtil.toTimestamp(startDate), TimeUtil.toTimestamp(endDate),
				logType, clientType, timeASC, page);
	}

	@Override
	public List<ResponseStatusLog> getResponseStatusLogs(Date startDate,
			Date endDate, ClientType clientType, boolean timeASC, Integer page) {
		return responseStatusLogDao.getListByTimestamp(
				TimeUtil.toTimestamp(startDate), TimeUtil.toTimestamp(endDate),
				clientType, timeASC, page);
	}

	public void setLogDao(ResponseStatusLogDao logDao) {
		this.responseStatusLogDao = logDao;
	}

	// 判断json数据是否含无效信息
	private boolean isInvalid(JSONObject json) {
		return contentValue(json, "invaliduser")
				|| contentValue(json, "invalidparty");
	}

	// json数据是否含有该key的值
	private boolean contentValue(JSONObject json, String key) {
		if (json == null) {
			return false;
		}
		return json.has(key) && Strings.isNullOrEmpty(json.getString(key));
	}
}
